package org.jooq.impl;

import org.jooq.Field;
import org.jooq.TableField;
import sf.core.DBField;
import sf.jooq.tables.JooqTable;

import java.util.Collection;
import java.util.Map;

public class JooqRecord extends AbstractRecord implements InternalRecord {

    private JooqTable<?> jooqTable;

    public JooqRecord() {
    }

    /**
     * Generated UID
     */
    private static final long serialVersionUID = -2201346180421463830L;

    JooqRecord(AbstractRow row) {
        super(row);
    }

    @Deprecated
    JooqRecord(Collection<? extends Field<?>> fields) {
        super(fields);
    }

    JooqRecord(RowImplN fields) {
        super(fields);
    }

    // ------------------------------------------------------------------------
    // XXX: Type-safe Record APIs
    // ------------------------------------------------------------------------

    @Override
    public RowImplN fieldsRow() {
        return new RowImplN(fields.fields);
    }

    @Override
    public final RowImplN valuesRow() {
        return new RowImplN(Tools.fields(values, fields.fields.fields));
    }

    public void setValue(DBField field, Object value) {
        Map<DBField, ?> map = jooqTable.getMap();
        TableField<?, Object> tf = (TableField<?, Object>) map.get(field);
        setValue(tf, value);
    }

    public Object getValue(DBField field) {
        Map<DBField, ?> map = jooqTable.getMap();
        TableField<?, ?> tf = (TableField<?, ?>) map.get(field);
        return get(tf);
    }

    public JooqTable<?> getJooqTable() {
        return jooqTable;
    }

    void setJooqTable(JooqTable<?> jooqTable) {
        this.jooqTable = jooqTable;
    }
}
